<template>
  <div style="padding: 6px;">
    <el-form ref="searchForm" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item label="角色名称">
            <el-input v-model="sd.roleName" size="small" placeholder="角色名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-search" @click="searchRole">查询</el-button>
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-plus" @click="addRole">添加</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="roleObj.list" border style="width: 100%">
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="mini" type="primary" @click="editRole(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger"  @click="deleteRole(scope.row.id)">删除</el-button>
          <el-button size="mini" type="primary" @click="roleSetPermission(scope.row.id)">权限设置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="center" background layout="total, prev, pager, next"
                   @current-change="loadRole" :total="roleObj.total"
                   :current-page.sync="sd.pageNum" :page-size="sd.pageSize">
    </el-pagination>

    <el-dialog :title="popupTitle" :visible.sync="entityDetailVisible" width="800px">
      <RoleDetail v-if="entityDetailVisible" :visible.sync="entityDetailVisible" :entityId="popupEntityId"></RoleDetail>
    </el-dialog>
    <el-dialog :title="popupTitle" :visible.sync="rolePermissionVisible" width="880px">
      <RolePermission v-if="rolePermissionVisible" :visible.sync="rolePermissionVisible" :roleId="popupEntityId"></RolePermission>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import RoleDetail from '@/views/sys/role/roleDetail'
import RolePermission from '@/views/sys/role/rolePermission'

export default {
  components: { RoleDetail, RolePermission },
  data() {
    return {
      sd: {
        roleName : '',
        pageNum: 1,
        pageSize: 5
      },
      roleObj: {},

      entityDetailVisible : false,
      popupTitle : '',
      popupEntityId : 0,

      rolePermissionVisible : false,
    }
  },
  mounted() {
    this.loadRole()
    this.$EventBus.$on("roleadd", () => {
      this.sd.pageNum = 1;
      this.loadRole();
    });
    this.$EventBus.$on("roleedit", () => {
      this.loadRole();
    });
  },
  beforeDestroy() {
    this.$EventBus.$off("roleadd")
    this.$EventBus.$off("roleedit")
  },
  methods: {
    searchRole(){
      this.sd.pageNum = 1;
      this.loadRole();
    },
    loadRole() {
      request({
        url: '/sys/role/pagerole',
        data: this.sd
      }).then(res => {
        const { data } = res
        this.roleObj = data
      })
    },
    addRole(){
      this.popupEntityId = 0;
      this.popupTitle= "新增角色"
      this.entityDetailVisible= true
    },
    editRole(id){
      this.popupEntityId= id;
      this.popupTitle= "修改角色"
      this.entityDetailVisible= true
    },
    roleSetPermission(id){
      this.popupEntityId= id;
      this.popupTitle= "权限设置"
      this.rolePermissionVisible= true
    },
    deleteRole(id){
      request({
        url: '/sys/role/deleterole',
        data: {id : id}
      }).then(res => {
        if(res.code === '200') {
          this.$message.success(res.msg)
          this.loadRole();
        }
      })
    }

  }
}
</script>
